/* 
 * Copyright (c) 2002 Boris Pruessmann and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Common Public License v0.5
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors:
 *    Boris Pruessmann - Initial implementation. 
 */
package net.sourceforge.perforce.core.internal.api;

import java.util.ArrayList;
import java.util.List;

import net.sourceforge.perforce.core.PerforceCorePlugin;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.MultiStatus;
import org.eclipse.core.runtime.Path;

/**
  * @version 	$Revision: 67 $  
  * @author 	<a href="mailto:bpruessmann@users.sourceforge.net">Boris Pruessmann</a>
  */
class SyncCommand extends AbstractPerforceCommand 
{
	// --------------------------------------------------------------------------- Private Variables
		
	/** Stores the result list. */
	private List result = new ArrayList();
	
	// --------------------------------------------------------------- Public Static Factory Methods
	
	/** Call this method to create a new SyncCommand instance.
	  *
	  * @param monitor the progress monitor.
	  * 
	  * @return the newly created SyncCommand. 
	  */
	public static final SyncCommand newInstance(IProgressMonitor monitor)
	{
		MultiStatus status = new MultiStatus(PerforceCorePlugin.ID, IStatus.OK, "", null); 
		return new SyncCommand(status, monitor);
	}
	
	// ------------------------------------------------------------------------ Private Constructors
	
	/** Constructor for SyncCommand.
	  *
	  * @param status the status object.
	  * @param monitor the progress monitor. 
	  */
	private SyncCommand(MultiStatus status, IProgressMonitor monitor)
	{
		super(status, monitor);

	}
	
	// ------------------------------------------------------------------------------ Public Methods

	/** @see AbstractPerforceCommand#outputInfo(char, String) */
	public void outputInfo(char level, String infoString)	
	{
		super.outputInfo(level, infoString);
		
		IWorkspaceRoot root = PerforceCorePlugin.getWorkspace().getRoot();
		
		int pos = infoString.indexOf("- added as");
		if (pos != -1)
		{
			String file = infoString.substring(pos + 11);
			getProgressMonitor().subTask(file);
			
			IFile fileHandle = root.getFileForLocation(new Path(file));
			if (fileHandle != null)
			{
				result.add(fileHandle);
			}
			
			return;
		}
		
		pos = infoString.indexOf("- updating");
		if (pos != -1)
		{
			String file = infoString.substring(pos + 11);
			getProgressMonitor().subTask(file);
			
			IFile fileHandle = root.getFileForLocation(new Path(file));
			if (fileHandle != null)
			{
				result.add(fileHandle);
			}
			
			return;
		}
		
		pos = infoString.indexOf("- deleted as");
		if (pos != -1)
		{
			String file = infoString.substring(pos + 13);
			getProgressMonitor().subTask(file);
			
			IFile fileHandle = root.getFileForLocation(new Path(file));
			if (fileHandle != null)
			{
				result.add(fileHandle);
			}
			
			return;
		}

		System.err.println(infoString);
	}

	/** @see IPerforceCommand#getCommand() */
	public String getCommand() 
	{
		return "sync";                                                                 //$NON-NLS-1$
	}
	
	/** Call this method to retrieve the result list.
	  *
	  * @return the result list. 
	  */
	public List getResult()
	{
		return result;
	}
}
